// pages/user/updateInfo/updateInfo.ts
import User, { UserInfo } from "../../../utils/user";
import { getGradeClasses } from "../../../utils/request/api";
import msg from "../../../utils/msg";
var user = new User;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo:user.getInfo() as UserInfo,
    classes : [] as Array<{text:string,value:string|number,children:Array<{text:string,value:string | number}>}>,
    columns: [
      {
        values: [] as Array<string>,
        className: 'column1',
      },
      {
        values: [] as Array<string>,
        className: 'column2',
        defaultIndex: 0,
      },
    ],
    show: false,
  },
  //提交
  submit:async function(){
    console.log(1);
    msg.Loading('上传中');
    await user.updateInfo(this.data.userInfo)
    .then((code:any)=>{ 
      msg.hideLoading();
      if(code==200){
        setTimeout(function(){
          wx.navigateBack();
        },1500)
      }
    });
  },
  //获取输入
  getInput(e:any){
    let index = e.currentTarget.dataset.index;
    this.setData({
      ['userInfo.'+index]:e.detail.value,
    });
  },
  //多级选择完成
  onFinish(e:any){
    let class_id=e.detail.value;
    let className = e.detail.selectedOptions[0].text+e.detail.selectedOptions[1].text;
    let userInfo = this.data.userInfo;
    userInfo.className=className;
    userInfo.class_id=parseInt(class_id);
    this.setData({
      userInfo:userInfo,
    });
  },
  onClose(){
    this.setData({
      show:false,
    })
  },
  chooseClass(){
    this.setData({
      show:true,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function() {
    let self = this;
    //拿取年级-班级数据
    await getGradeClasses()
    .then((res:any)=>{
      self.setData({
        classes:res.data.data,
      })
    });
  },
  onClickLeft(){
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let user1 = new User;
    this.setData({
      userInfo:user1.getInfo(),
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})